package com.glsc.ngateway.opmanage.controller.xinyi;


import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.log.XinYiLog;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.dto.XinYiDto;
import com.glsc.ngateway.opmanage.service.xinyi.XinYiService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * oa新意指令执行记录以及重试
 */
@RestController
@RequestMapping("/api/xinyi")
@Slf4j
public class XinYiController {

    @Resource
    private UserCache userCache;

    @Resource
    private XinYiService xinYiService;

    /**
     * 分页查找
     *
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<XinYiLog>> pageTemplate(@RequestBody XinYiDto dto, HttpServletRequest request) {

        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(dto.getOaFlowName())) {
            param.put("LIKE_oaFlowName", dto.getOaFlowName());
        }
        if (StringUtils.isNotBlank(dto.getMemo())) {
            param.put("LIKE_memo", dto.getMemo());
        }
        Page<XinYiLog> page = xinYiService.page(dto.getPageNo(), dto.getPageSize(), param);

        return PlatformResponse.successData(page);
    }

    /**
     * 重新触发新意指令
     *
     * @param
     * @return
     */
    @GetMapping("/retry")
    public PlatformResponse<?> findTemplateById(@ApiParam(required = false, value = "id") @RequestParam(required = true) Long id) {
        XinYiLog retry = xinYiService.retry(id);
        if (Objects.isNull(retry) || "N".equals(retry.getStatus())) {
            return PlatformResponse.failedMsg("触发失败");
        }
        return PlatformResponse.success();
    }


}
